package com.itheima.web;

import com.itheima.dto.QueryMonth;
import com.itheima.dto.SetmealDto;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import com.itheima.support.PageResult;
import com.itheima.support.QueryPageBean;
import com.itheima.support.Result;
import com.itheima.utils.ExcelUtils;
import com.itheima.utils.ReportExcelDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class ReportController {

    @DubboReference
    private ReportService reportService;

    /*
     * 会员数量折线图
     * */
    @GetMapping("/totalMemberCounts")
    public Result totalMemberCounts(){
        return new Result(reportService.totalMemberCounts());
    }

    /*
     * 套餐占比饼状图
     * */
    @GetMapping("/orderSetmealProportion")
    public Result orderSetmealProportion(){
        return new Result(reportService.orderSetmealProportion());
    }

    /*
     * 按时间段查询会员数量
     * */
    @PostMapping("/getMemberCountsByMonth")
    public Result getMemberCountsByMonth(@RequestBody QueryMonth queryMonth) {
        String startMonth = queryMonth.getStartMonth();
        String endMonth = queryMonth.getEndMonth();
        return new Result(reportService.getMemberCountsByMonth(startMonth,endMonth));
    }

    /*
     * 会员性别比例饼状图
     * */
    @GetMapping("/memberSexProportion")
    public Result memberSexProportion(){
        return new Result(reportService.memberSexProportion());
    }

    /*
     * 会员年龄段占比饼状图
     * */
    @GetMapping("/memberAgeProportion")
    public Result memberAgeProportion(){
        return new Result(reportService.memberAgeProportion());
    }

    @ResponseBody
    @GetMapping("/initReportData")
    public Result initReportData() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer todayNewMember = reportService.todayNewMember();
        Integer totalMember = reportService.totalMember();
        Integer thisWeekNewMember = reportService.thisWeekNewMember();
        Integer thisMonthNewMember = reportService.thisMonthNewMember();
        Integer todayOrderNumber = reportService.todayOrderNumber();
        Integer todayVisitsNumber = reportService.todayVisitsNumber();
        Integer thisWeekOrderNumber = reportService.thisWeekOrderNumber();
        Integer thisWeekVisitsNumber = reportService.thisWeekVisitsNumber();
        Integer thisMonthOrderNumber = reportService.thisMonthOrderNumber();
        Integer thisMonthVisitsNumber = reportService.thisMonthVisitsNumber();
        List<Map<String, String>> hotSetmeal = reportService.hotSetmeal();

        Map<String, Object> result = new HashMap<>();
        result.put("reportDate", sdf.format(new Date()));
        result.put("todayNewMember", todayNewMember);
        result.put("totalMember", totalMember);
        result.put("thisWeekNewMember", thisWeekNewMember);
        result.put("thisMonthNewMember", thisMonthNewMember);
        result.put("todayOrderNumber", todayOrderNumber);
        result.put("todayVisitsNumber", todayVisitsNumber);
        result.put("thisWeekOrderNumber", thisWeekOrderNumber);
        result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        result.put("thisMonthOrderNumber", thisMonthOrderNumber);
        result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        result.put("hotSetmeal", hotSetmeal);
        return new Result(result);
    }

    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer todayNewMember = reportService.todayNewMember();
        Integer totalMember = reportService.totalMember();
        Integer thisWeekNewMember = reportService.thisWeekNewMember();
        Integer thisMonthNewMember = reportService.thisMonthNewMember();
        Integer todayOrderNumber = reportService.todayOrderNumber();
        Integer todayVisitsNumber = reportService.todayVisitsNumber();
        Integer thisWeekOrderNumber = reportService.thisWeekOrderNumber();
        Integer thisWeekVisitsNumber = reportService.thisWeekVisitsNumber();
        Integer thisMonthOrderNumber = reportService.thisMonthOrderNumber();
        Integer thisMonthVisitsNumber = reportService.thisMonthVisitsNumber();
        List<Map<String, String>> hotSetmeal = reportService.hotSetmeal();

        ReportExcelDto reportExcelDto = ReportExcelDto.builder()
                .reportDate(sdf.format(new Date()))
                .todayNewMember(todayNewMember)
                .totalMember(totalMember)
                .thisWeekNewMember(thisWeekNewMember)
                .thisMonthNewMember(thisMonthNewMember)
                .todayOrderNumber(todayOrderNumber)
                .todayVisitsNumber(todayVisitsNumber)
                .thisWeekOrderNumber(thisWeekOrderNumber)
                .thisWeekVisitsNumber(thisWeekVisitsNumber)
                .thisMonthOrderNumber(thisMonthOrderNumber)
                .thisMonthVisitsNumber(thisMonthVisitsNumber)
                .build();

        ExcelUtils.writeExcel(response, reportExcelDto, hotSetmeal);
    }

}
